#include <linux/types.h>

#ifdef CONFIG_SONYPI_COMPAT
#include <linux/poll.h>
#include <linux/miscdevice.h>

/******** SONYPI compatibility **********/

static int minor = -1;
module_param(minor, int, 0);
MODULE_PARM_DESC(minor,
		 "minor number of the misc device for the SPIC compatibility code, "
		 "default is -1 (automatic)");

/* battery / brightness / temperature  addresses */
#define SONYPI_BAT_FLAGS	0x81
#define SONYPI_LCD_LIGHT	0x96
#define SONYPI_BAT1_PCTRM	0xa0
#define SONYPI_BAT1_LEFT	0xa2
#define SONYPI_BAT1_MAXRT	0xa4
#define SONYPI_BAT2_PCTRM	0xa8
#define SONYPI_BAT2_LEFT	0xaa
#define SONYPI_BAT2_MAXRT	0xac
#define SONYPI_BAT1_MAXTK	0xb0
#define SONYPI_BAT1_FULL	0xb2
#define SONYPI_BAT2_MAXTK	0xb8
#define SONYPI_BAT2_FULL	0xba
#define SONYPI_TEMP_STATUS	0xC1


/* get/set brightness */
#define SONYPI_IOCGBRT		_IOR('v', 0, __u8)
#define SONYPI_IOCSBRT		_IOW('v', 0, __u8)

/* get battery full capacity/remaining capacity */
#define SONYPI_IOCGBAT1CAP	_IOR('v', 2, __u16)
#define SONYPI_IOCGBAT1REM	_IOR('v', 3, __u16)
#define SONYPI_IOCGBAT2CAP	_IOR('v', 4, __u16)
#define SONYPI_IOCGBAT2REM	_IOR('v', 5, __u16)

/* get battery flags: battery1/battery2/ac adapter present */
#define SONYPI_BFLAGS_B1	0x01
#define SONYPI_BFLAGS_B2	0x02
#define SONYPI_BFLAGS_AC	0x04
#define SONYPI_IOCGBATFLAGS	_IOR('v', 7, __u8)

/* get/set bluetooth subsystem state on/off */
#define SONYPI_IOCGBLUE		_IOR('v', 8, __u8)
#define SONYPI_IOCSBLUE		_IOW('v', 9, __u8)

/* get/set fan state on/off */
#define SONYPI_IOCGFAN		_IOR('v', 10, __u8)
#define SONYPI_IOCSFAN		_IOW('v', 11, __u8)

/* get temperature (C) */
#define SONYPI_IOCGTEMP		_IOR('v', 12, __u8)

struct sonypi_compat_s {
	struct fasync_struct* fifo_async;
	struct kfifo          fifo;
	spinlock_t            fifo_lock;
	wait_queue_head_t     fifo_proc_list;
	atomic_t              open_count;
};
static struct sonypi_compat_s sonypi_compat = {
	.open_count = ATOMIC_INIT(0),
};

static int sonypi_misc_fasync(int fd, struct file *filp, int on){
	return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
}

static int sonypi_misc_release(struct inode *inode, struct file *file){
	atomic_dec(&sonypi_compat.open_count);
	return 0;
}

static int sonypi_misc_open(struct inode *inode, struct file *file){
	/* Flush input queue on first open */
	unsigned long flags;
	spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
	if (atomic_inc_return(&sonypi_compat.open_count) == 1) kfifo_reset(&sonypi_compat.fifo);
	spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
	return 0;
}

static ssize_t sonypi_misc_read(struct file *file, char __user *buf, size_t count, loff_t *pos){
	ssize_t ret;
	unsigned char c;

	if( (kfifo_len(&sonypi_compat.fifo) == 0) && (file->f_flags & O_NONBLOCK) ) return -EAGAIN;

	ret = wait_event_interruptible( sonypi_compat.fifo_proc_list, kfifo_len(&sonypi_compat.fifo) != 0 );
	if( ret ) return ret;

	while (ret < count &&
	       (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
			  &sonypi_compat.fifo_lock) == sizeof(c))) {
		if( put_user(c, buf++) ) return -EFAULT;
		ret++;
	}

	if (ret > 0) {
		struct inode *inode = file_inode(file);
		inode->i_atime = current_time(inode);
	}

	return ret;
}

static __poll_t sonypi_misc_poll(struct file *file, poll_table *wait)
{
	poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
	if (kfifo_len(&sonypi_compat.fifo))
		return EPOLLIN | EPOLLRDNORM;
	return 0;
}

static int ec_read16( u8 addr, u16 *value ){
	u8 val_lb, val_hb;
	if( ec_read(addr, &val_lb) ) return -1;
	if( ec_read(addr + 1, &val_hb) ) return -1;
	*value = val_lb | (val_hb << 8);
	return 0;
}

static long sonypi_misc_ioctl( struct file *fp, unsigned int cmd, unsigned long arg ){
	int ret = 0;
	void __user *argp = (void __user *)arg;
	u8 val8;
	u16 val16;
	int value;

	mutex_lock(&spic_dev.lock);
	switch(cmd){
		case SONYPI_IOCGBRT:
			if (sony_bl_props.dev == NULL) {
				ret = -EIO;
				break;
			}
			if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
						&value)) {
				ret = -EIO;
				break;
			}
			val8 = ((value & 0xff) - 1) << 5;
			if (copy_to_user(argp, &val8, sizeof(val8)))
					ret = -EFAULT;
			break;
		case SONYPI_IOCSBRT:
			if (sony_bl_props.dev == NULL) {
				ret = -EIO;
				break;
			}
			if (copy_from_user(&val8, argp, sizeof(val8))) {
				ret = -EFAULT;
				break;
			}
			value = (val8 >> 5) + 1;
			if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
						NULL)) {
				ret = -EIO;
				break;
			}
			/* sync the backlight device status */
			sony_bl_props.dev->props.brightness =
				sony_backlight_get_brightness(sony_bl_props.dev);
			break;
		case SONYPI_IOCGBAT1CAP:
			if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
				ret = -EIO;
				break;
			}
			if (copy_to_user(argp, &val16, sizeof(val16)))
				ret = -EFAULT;
			break;
		case SONYPI_IOCGBAT1REM:
			if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
				ret = -EIO;
				break;
			}
			if (copy_to_user(argp, &val16, sizeof(val16)))
				ret = -EFAULT;
			break;
		case SONYPI_IOCGBAT2CAP:
			if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
				ret = -EIO;
				break;
			}
			if (copy_to_user(argp, &val16, sizeof(val16)))
				ret = -EFAULT;
			break;
		case SONYPI_IOCGBAT2REM:
			if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
				ret = -EIO;
				break;
			}
			if (copy_to_user(argp, &val16, sizeof(val16)))
				ret = -EFAULT;
			break;
		case SONYPI_IOCGBATFLAGS:
			if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
				ret = -EIO;
				break;
			}
			val8 &= 0x07;
			if (copy_to_user(argp, &val8, sizeof(val8)))
				ret = -EFAULT;
			break;
		case SONYPI_IOCGBLUE:
			val8 = spic_dev.bluetooth_power;
			if (copy_to_user(argp, &val8, sizeof(val8)))
				ret = -EFAULT;
			break;
		case SONYPI_IOCSBLUE:
			if (copy_from_user(&val8, argp, sizeof(val8))) {
				ret = -EFAULT;
				break;
			}
			__sony_pic_set_bluetoothpower(val8);
			break;
		/* FAN Controls */
		case SONYPI_IOCGFAN:
			if (sony_pic_get_fanspeed(&val8)) {
				ret = -EIO;
				break;
			}
			if (copy_to_user(argp, &val8, sizeof(val8)))
				ret = -EFAULT;
			break;
		case SONYPI_IOCSFAN:
			if (copy_from_user(&val8, argp, sizeof(val8))) {
				ret = -EFAULT;
				break;
			}
			if (sony_pic_set_fanspeed(val8))
				ret = -EIO;
			break;
		/* GET Temperature (useful under APM) */
		case SONYPI_IOCGTEMP:
			if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
				ret = -EIO;
				break;
			}
			if (copy_to_user(argp, &val8, sizeof(val8)))
				ret = -EFAULT;
			break;
		default:
			ret = -EINVAL;
	}
	mutex_unlock(&spic_dev.lock);
	return ret;
}

static const struct file_operations sonypi_misc_fops = {
	.owner          = THIS_MODULE,
	.read           = sonypi_misc_read,
	.poll           = sonypi_misc_poll,
	.open           = sonypi_misc_open,
	.release        = sonypi_misc_release,
	.fasync         = sonypi_misc_fasync,
	.unlocked_ioctl = sonypi_misc_ioctl,
	.llseek         = noop_llseek,
};

static struct miscdevice sonypi_misc_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name  = KBUILD_MODNAME,
	.fops  = &sonypi_misc_fops,
};

void sonypi_compat_report_event( u8 event ){
	kfifo_in_locked( &sonypi_compat.fifo, (unsigned char *)&event, sizeof(event), &sonypi_compat.fifo_lock );
	kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
	wake_up_interruptible(&sonypi_compat.fifo_proc_list);
}

int sonypi_compat_init( void ){
	int error;

	spin_lock_init(&sonypi_compat.fifo_lock);
	error = kfifo_alloc( &sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL );
	if( error ){
		pr_err("kfifo_alloc failed\n");
		return error;
	}

	init_waitqueue_head(&sonypi_compat.fifo_proc_list);

	if( minor != -1 ) sonypi_misc_device.minor = minor;
	error = misc_register(&sonypi_misc_device);
	if( error ){
		pr_err("misc_register failed\n");
		goto err_free_kfifo;
	}
	if( minor == -1 ) pr_info( "device allocated minor is %d\n", sonypi_misc_device.minor );
	
	return 0;
	
err_free_kfifo:
	kfifo_free(&sonypi_compat.fifo);
	return error;
}

void sonypi_compat_exit(void){
	misc_deregister(&sonypi_misc_device);
	kfifo_free(&sonypi_compat.fifo);
}
#else
int sonypi_compat_init(void) { return 0; }
void sonypi_compat_exit(void) { }
void sonypi_compat_report_event(u8 event) { }
#endif /* CONFIG_SONYPI_COMPAT */
